/*this program defines the linkist stack without headnode*/
//#include "stack2.h"
#include"status.h"
#include "dat2.h"
#include <stdio.h>
#include <stdlib.h>
status initstack(stack **s){
	(*s)=NULL;
	return ok;

}
status destroy(stack *s){
	stack *p;
	while(s){
		p=s->next;
		free(s);
		s=p;
	}
	return ok;
}

status is_empty(stack *s){
	if(s)
		return false;
	else 
		return true;
}

int get_length(stack *s){
	int i=0;
	stack *p=s;
	while(p){
		p=p->next;
		i++;
	}
	return i;
}

status get_top(stack *s,redtype **e){
	redtype tmp=s->data;
	*e=&tmp;
	return ok;
}
status clear_stack(stack *s){
	stack *p,*q;
	p=s;
	while(p){
		q=p->next;
		free(p);
		p=q;
	}
	return ok;
}

status push(stack **s,redtype e){
	stack* node=(stack*)malloc(sizeof(stack));
	if(!node) exit(overflow);
	node->data=e;
	node->next=*s;
	*s=node;
}

status pop(stack **s,redtype **e){
	if(!*s)
		exit(underflow);
	*e=&((*s)->data);
	*s=(*s)->next;
	return ok;
}

int main(){
    stack *s;
	initstack(&s);
	printf("is empty:%d\n",is_empty(s));
	redtype r1;
	r1.elem='a';
	redtype r2;
	r2.elem='b';
	redtype r3;
	r3.elem='c';
	redtype r4;
	r4.elem='d';
	redtype r5;
	r5.elem='e';
	push(&s,r1);
	push(&s,r2);
	push(&s,r3);
	push(&s,r4);
	push(&s,r5);
	printf("stack length:%d\n",get_length(s));
	redtype *r6;
	get_top(s,&r6);
	printf("get top:%c\n",r6->elem);
	redtype *r7;
	pop(&s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(&s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(&s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(&s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(&s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(&s,&r7);
	printf("pop:%c\n",r7->elem);
	pop(&s,&r7);
	printf("pop:%c\n",r7->elem);
}
